package com.zhledu.datastructure.stack;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.*;

public class BracketsPatch {

    private static Queue<Integer> queue = new LinkedList<>();
    private static int num = 0;
    private static boolean workFlag = true;
    private static boolean stopFlag = false;
    private static int MAX_NUM = 100;

    public static void main(String[] args) throws Exception {
     /*   BufferedReader reader = new BufferedReader(new FileReader(new File("/Users/zhli/工作空间/IDEA/study/src/main/java/com/zhledu/datastructure/List/MyLinkedList.java")));
        StringBuffer sb = new StringBuffer();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line);
        }
        reader.close();
        System.out.println(bracketsPatch(sb.toString()));*/
        bankQueueUp();
    }

    public static boolean bracketsPatch(String text) {
        Stack<String> stack = new Stack<>();
        String left = "{[(";
        String right = "}])";
        boolean flag = true;
        for (int i = 0; i < text.length(); i++) {
            String current = String.valueOf(text.charAt(i));
            if (left.contains(current)) {
                stack.push(current);
                continue;
            }
            if (right.contains(current)) {
                if (stack.empty()) {
                    flag = false;
                    break;
                } else {
                    String top = stack.pop();
                    int index = right.indexOf(current);
                    if (top.equals(String.valueOf(left.charAt(index)))) {
                        continue;
                    } else {
                        flag = false;
                        break;
                    }
                }
            }
        }
        if (!stack.empty()) {
            flag = false;
        }
        return flag;
    }

    private static void printInfo() {
        String info = "============================================================\n" +
                "                                欢迎使用银行排队叫号服务\n" +
                "请选择操作：\n" +
                "1：取号\n" +
                "2：叫号\n" +
                "3：下班\n" +
                "4：上班\n" +
                "5：查看队列\n" +
                "6：停止叫号\n" +
                "7：退出\n" +
                "==========================================================\n" +
                "请输入需要的操作：";
        System.out.println(info);
    }

    private static void view() {
        int count = 0;
        if (queue.isEmpty()) {
            System.out.println("没有顾客排队");
        } else {
            Iterator<Integer> iterator = queue.iterator();
            while (iterator.hasNext()) {
                System.out.println("第" + (++count) + "位：" + iterator.next() + "号");
            }
        }
    }

    public static void bankQueueUp() {
        printInfo();
        int oper = 0;
        Scanner scanner = new Scanner(System.in);
        while (true) {
            oper = scanner.nextInt();
            switch (oper) {
                case 1: {
                    if (workFlag) {
                        if (stopFlag) {
                            System.out.println("当前已经停止叫号，请下次再来办理业务");
                        } else {
                            if (num < MAX_NUM) {
                                queue.offer(++num);
                                System.out.println("您的号码为" + num + ",您前面还有" + (queue.size() - 1) + "位等待。");
                            } else {
                                System.out.println("当天号已经取完，请下次办理业务");
                            }
                        }
                    } else {
                        System.out.println("当前已经下班，不再办理业务");
                    }
                    break;
                }
                case 2: {
                    if (!queue.isEmpty()) {
                        int head = queue.poll();
                        System.out.println("请" + head + "号到柜台办理业务！");
                    } else {
                        System.out.println("当前没有顾客等待");
                    }
                    break;
                }
                case 3: {
                    if (!workFlag) {
                        System.out.println("当前正处于下班状态，不允许做下班操作");
                    } else {
                        workFlag = false;
                        queue.clear();
                        num = 0;
                    }
                    break;
                }
                case 4: {
                    if (workFlag) {
                        System.out.println("当前正在上班，不允许做上班操作");
                    } else {
                        workFlag = true;
                        queue.clear();
                        num = 0;
                    }
                    break;
                }
                case 5: {
                    view();
                    break;
                }

                case 6: {
                    if (!stopFlag) {
                        stopFlag = true;
                    }
                    break;
                }
                case 7: {
                    System.exit(0);
                }
                default: {
                    break;
                }
            }
            printInfo();
        }
    }

}


